package cn.stylefeng.guns.modular.gridsystem.temperature.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizDevice;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGateway;
import cn.stylefeng.guns.modular.gridsystem.service.BizDeviceService;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayService;
import cn.stylefeng.guns.modular.gridsystem.temperature.entity.BizTemperatureChannel;
import cn.stylefeng.guns.modular.gridsystem.temperature.entity.BizTemperatureRecord;
import cn.stylefeng.guns.modular.gridsystem.temperature.mapper.BizTemperatureChannelMapper;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.param.BizTemperatureChannelParam;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.result.BizTemperatureAllResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.model.result.BizTemperatureChannelResult;
import cn.stylefeng.guns.modular.gridsystem.temperature.service.BizTemperatureChannelService;
import cn.stylefeng.guns.modular.gridsystem.temperature.service.BizTemperatureRecordService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BizTemperatureChannelServiceImpl extends ServiceImpl<BizTemperatureChannelMapper, BizTemperatureChannel> implements BizTemperatureChannelService {
    @Autowired
    private BizTemperatureRecordService temperatureRecordService;

    @Override
    public BizTemperatureAllResult allList(BizTemperatureChannelParam param) {
        //查询设备
        List<BizDevice> deviceList = SpringUtil.getBean(BizDeviceService.class).list();
        //查询网关 网关状态(1开启，0禁用）
        List<BizGateway> gatewayList = SpringUtil.getBean(BizGatewayService.class).lambdaQuery().eq(BizGateway::getStatus, 1).list();
        BizTemperatureAllResult allResult = new BizTemperatureAllResult();
        allResult.setDeviceList(deviceList);
        allResult.setGatewayList(gatewayList);
        return allResult;
    }

    @Override
    public PageResult<BizTemperatureChannelResult> getTempChannelList(BizTemperatureChannelParam param) {
        Page<BizTemperatureChannelResult> page = this.baseMapper.getTempChannelList(PageFactory.defaultPage(), param);
        return new PageResult<>(page);
        //  return this.baseMapper.getTempChannelList(param);
    }

    @Override
    public void toAdd(BizTemperatureChannelParam param) {
        BizTemperatureChannel old = this.lambdaQuery()
                .eq(BizTemperatureChannel::getGatewayChannel, param.getGatewayChannel())
                .eq(BizTemperatureChannel::getGatewayId, param.getGatewayId())
               // .eq(BizTemperatureChannel::getDeviceId, param.getDeviceId())
                .one();
        if (ObjectUtil.isNotNull(old)) {
            throw new ServiceException("【" + param.getGatewayChannel() + "】通道号已存在!");
        }
        BizTemperatureChannel bizTemperatureChannel = new BizTemperatureChannel();
        BeanUtil.copyProperties(param, bizTemperatureChannel);
        BizGateway bizGateway = SpringUtil.getBean(BizGatewayService.class).getById(param.getGatewayId());
        bizTemperatureChannel.setGatewayCode(bizGateway.getGatewayCode());
        this.save(bizTemperatureChannel);
    }

    @Override
    public void toEdit(BizTemperatureChannelParam param) {
        BizTemperatureChannel old = this.getById(param.getId());
        if (ObjectUtil.isNotNull(old)) {
            //通道号不一样
            if (!param.getGatewayChannel().equals(old.getGatewayChannel())) {
                //新的通道号是否占用
                BizTemperatureChannel old2 = this.lambdaQuery()
                        .eq(BizTemperatureChannel::getGatewayChannel, param.getGatewayChannel())
                        .eq(BizTemperatureChannel::getGatewayId, param.getGatewayId())
                       // .eq(BizTemperatureChannel::getDeviceId, param.getDeviceId())
                        .one();
                if (ObjectUtil.isNotNull(old2)) {
                    throw new ServiceException("【" + param.getGatewayChannel() + "】通道号已经被占用!");
                }
            }
        }
        //修改温度记录表中首次添加未关联设备id
        final LambdaUpdateWrapper<BizTemperatureRecord> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BizTemperatureRecord::getGatewayId, param.getGatewayId());
        updateWrapper.eq(BizTemperatureRecord::getGatewayChannel, param.getGatewayChannel());
//        updateWrapper.eq(BizTemperatureRecord::getDeviceId, null);
        updateWrapper.isNull(BizTemperatureRecord::getDeviceId);
        updateWrapper.set(BizTemperatureRecord::getDeviceId, param.getDeviceId());
        updateWrapper.set(BizTemperatureRecord::getInstallLocation, param.getInstallLocation());
        updateWrapper.set(BizTemperatureRecord::getMaxValue, param.getMaxValue());
        temperatureRecordService.update(updateWrapper);
        BeanUtil.copyProperties(param, old);
        this.updateById(old);
    }


    @Override
    public void toDelete(BizTemperatureChannelParam param) {
        BizTemperatureChannel bizTemperatureChannel = this.getById(param.getId());
        if (ObjectUtil.isNotNull(bizTemperatureChannel)) {
            this.removeById(param.getId());
        }
    }


}
